<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      >
<head th:replace="fragments/tabhead::headc"></head>
<body>
<th:block layout:fragment="content">
<div style="margin-top:15px">
	  <form class="layui-form search" id="searchForm">
	  <input type="hidden" id="type" class="layui-input" name='type' th:value="${type}"/>
	  <div class="layui-form-item layui-elem-quote">
	    <div class="layui-inline" >
	      <label class="layui-form-label" style="width:100px;">所属公司</label>
	      <div class="layui-input-inline" style="width:200px">
	      	 <select  id="companyId" name="companyId">
	      	 	<option value="">请选择公司</option>
	<!--        <option th:each="ec:${@redisHelper.getActCenterCompany(session.userCompanyId)}" th:value="${ec.id}" th:text="${ec.name}"></option> -->
				<option th:each="ec:${actCenterCompanyMap}" th:value="${ec.id}" th:text="${ec.name}"></option>
	         </select>
	      </div>
	    </div>
	    <div class="layui-inline" >
	      <label class="layui-form-label" style="width:80px;">名称:</label>
	      <div class="layui-input-inline" style="width:130px;">
	        <input type="text" id="name" class="layui-input" name='name' placeholder="请输入名称"/>
	      </div>
	    </div>
	    <div class="layui-inline">
	      <div class="layui-input-inline" style="width:400px;">
	        <button class="layui-btn" id="searchBtn" type="button">查询</button>
	        <button class="layui-btn" id="addBtn" type="button" authcode="900319">新增</button>
	      </div>
	    </div>
	  </div>
	 </form>
</div>
<table class="layui-table"  id="tab" lay-filter="tab"></table>
<script>
var ins,trobj,editdialogindex;
$(function() {
	// start table use
	layui.use('table',function() {
		var table = layui.table;
		//tart table render
	ins=table.render({
			elem:'#tab'
			,url:'/com/supplier/customer/provider/data?first=true'
			,cols:[[
// 					  {field:'id',width:80, sort: true,title:'ID',fixed:'true'},
				      {field:'companyName',width:180,title:'公司名称'},
				      {field:'no', width:250,sort: true,title:'编码'},
				      {field:'name', width:250,title:'姓名'},
				      {field:'catagory',width:180,title:'分类',templet:'#catagoryTpl'},
				      {field:'address', width:250,title:'地址'},
				      {field:'phone', width:180,title:'手机号'},
				      {field:'departmentName', width:180,title:'分管部门'},
				      {field:'staffName', width:180,title:'分管员工'},
				      {field:'corporate', width:180,title:'法人'},
				      {field:'bank', width:180,title:'银行'},
				      {field:'bank_account', width:180,title:'银行账户'},
				      {field:'tax_rate', width:180,title:'税率',templet:'#taxTpl'},
				      {field:'business_license', width:180,title:'营业执照'},
				      {field:'advance_balance', width:180,title:'预付余额'},
				      {field:'payable_balance', width:180,title:'应付余额'},
				      {field:'ctime', width:180,sort: true,title:'创建日期'},
				      {field:'remark', width:180,title:'备注'},
				      {align:'center',fixed:'right',width:300, toolbar:'#toolbar',title:'操作'}
			     ]]
				,page: true //是否显示分页
			    ,limits: [10,20,50]
			    ,limit:10 //每页默认显示的数量
				,done:function(res,curr,count){
	// 			 console.log(res);
				 page = curr;
				    //得到当前页码
// 				    console.log(curr); 
				    //得到数据总量
// 				    console.log(count);
			}
		});//end table render
		//start table on 监听工具条
		table.on('tool(tab)',function(obj) {
			var data = obj.data; //获得当前行数据
			var layEvent = obj.event; //获得 lay-event 对应的值
			trobj = obj;
			var tr = obj.tr; //获得当前行 tr 的DOM对象
			if(layEvent == 'edit') {
				$.get('/com/supplier/customer/provider/edit?id='+data.id,{},function(str) {
					editdialogindex = layer.open({
						title:"供应商修改",
					    type: 1,
					    area: ['800px', '560px'],
					    content: str, //注意，如果str是object，那么需要字符拼接。
					    zIndex:10
					  });
				});
			}else if(layEvent == 'del') {
				 layer.confirm('确定删除该行单据', {icon: 3, title:'提示'}, function(index){
				    		 //向服务端发送删除指令
				    		$.get('/com/supplier/customer/provider/del_execute?id='+data.id,{},function(result) {
				    			if(result.code == 200) {
				    				trobj.del(); //删除对应行（tr）的DOM结构
								    layer.close(index);
								    layer.msg(result.message,{icon: 6});
								    return false;
				    			}else{
				    			 layer.msg(result.message,{icon: 0});
				    			 layer.close(index);
				    			 return false;
				    			} 
				    		});
				      
				      });
			}else if(layEvent == 'detail') {
				$.get('/com/supplier/customer/provider/view/?id='+data.id,{},function(str) {
					editdialogindex = layer.open({
						title:"供应商详情",
					    type: 1,
					    area: ['800px', '560px'],
					    content: str, //注意，如果str是object，那么需要字符拼接。
					    zIndex:10
					  });
				});
			}else if(layEvent == "contacts") {
		        var url="/com/provider/supplier/contacts/index?id="+data.id+"&companyName="+data.companyName+"&companyId="+data.company_id;
                menuOpen(url,"【"+data.name+"】联系人");	
			}else if(layEvent == "statement") {
				var companyId = $("select[name='companyId'] option:selected").val();
		        var url="/com/provider/supplier/statement/index?id="+data.id+"&companyName="+data.companyName+"&companyId="+data.company_id;
                menuOpen(url,"【"+data.name+"】结算方式");	
			}
		});//end table on
		 $("#searchBtn").click(function(){
			    ins.reload({url: '/com/supplier/customer/provider/data',where:$("#searchForm").serializeJson()})
			    return false;
			});  //end search
		$("#addBtn").click(function() {
			var companyId = $("#companyId option:selected").val();
			var type = $("#type").val();
			console.log("companyId="+companyId+"type="+type);
			$.get('/com/supplier/customer/provider/addinit?companyId='+companyId+'&newType='+type,{},function(str) {
				editdialogindex = layer.open({
					title:"供应商新增",
				    type: 1,
				    area: ['800px', '560px'],
				    content: str, //注意，如果str是object，那么需要字符拼接。
				    zIndex:10
				  });
			});
		});
	});//end table use
});
</script>
<script type="text/html" id="taxTpl">
{{#if (d.tax_rate !=null && d.tax_rate!=''){}}
<span>{{d.tax_rate}}%</span>
{{# }}}
</script>
<script type="text/html" id="catagoryTpl">
{{#if (d.catagory==0){}}
  		国内
{{# }else if(d.catagory==1) {}}
		国外
{{#  } }}
</script>
<script type="text/html" id="toolbar">
	<a class="layui-btn  layui-btn-mini" lay-event="detail">详情</a>
	<a class="layui-btn layui-btn-mini" lay-event="edit" authcode="900320">修改</a>
	<a class="layui-btn layui-btn-danger layui-btn-mini" lay-event="del" authcode="900321">删除</a>
	<a class="layui-btn  layui-btn-mini" lay-event="contacts" authcode="900322">联系人</a>
	<a class="layui-btn  layui-btn-mini" lay-event="statement" authcode="900326">结算方式</a>
</script>
</th:block>
</body>
</html>